//菜单树
angular.module('app').component('mMenuTree', {
    templateUrl: getTemplateUrl('matrix/component/tpl/mMenuTree.html'),
    bindings: {
        sys: '@',
        reqId: '@',
        onNodeSelect: '&'
    },
    controller: function ($scope, $element, $http, $attrs, $modal, $timeout, $rootScope) {
        var ctrl = this;
        ctrl.my_tree = {};
        ctrl.typeName = "新增";
        ctrl.selectedBranch = null;
        if (ctrl.sys == undefined) {
            ctrl.sys = 'TOM';
        }

        ctrl.my_tree_handler = function (branch) {
            console.log(ctrl.my_tree.get_selected_branch());
            ctrl.selectedBranch = ctrl.my_tree.get_selected_branch();
            ctrl.onNodeSelect({
                info: branch
            })
        };
        ctrl.load = function () {
            var url = matrix_product_url + '/menu/getMenu';
            if (this.reqId != undefined) {
                url = url + '?reqId=' + this.reqId;
            }
            $.ajax({
                type: "get",
                url: url,
                dataType: "JSON",
                async: false,
                success: function (data) {
                    ctrl.my_data = [data.data];
                    $timeout(function () {
                        if (ctrl.selectedBranch != null) {
                            ctrl.my_tree.set_select_branch_by_id(ctrl.selectedBranch.asParentId);
                        } else {
                            ctrl.my_tree.set_select_branch_by_id(ctrl.my_data[0].id);
                        }
                        return ctrl.my_tree.expand_all();
                    }, 1000);
                },
                complete: function (xhr, status) { }
            });
        }

        ctrl.load();

        ctrl.dataFun = function (info) {
            var obj = {}
            if (info.parent_id == '0') {
                info.level = '0';
            }
            info.children = info.child;
            obj.label = info.menu_data.name;
            obj.status = info.status;
            return obj;
        }

        ctrl.updatePop = function (info, status) {
            var self = this;
            var modalInstance = $modal.open({
                animation: true,
                templateUrl: 'winitMenuPagePop.html',
                controller: 'winitMenuPagePopCtrl',
                keyboard: true,
                backdrop: true,
                resolve: {
                    info: function () {
                        return info;
                    },
                    status: function () {
                        return status;
                    }
                }
            });

            modalInstance.result.then(function (result) {
                saveModal(result, status);
            }, function (reason) {
                cancelModal(reason);
            });
        }

        ctrl.try_adding_a_branch = function () {
            //set routeId
            var path = "";
            var crtId = ctrl.selectedBranch.id;
            if(ctrl.selectedBranch.level == "0") {
                path = "/" + ctrl.selectedBranch.menu_data.path;
            } else if(ctrl.selectedBranch.level == "1") {
                var fatherBranch = ctrl.my_tree.select_parent_branch();
                path = "/" + fatherBranch.menu_data.path + "/" + ctrl.selectedBranch.menu_data.path;
            } else if(ctrl.selectedBranch.level == "2") {
                var fatherBranch = ctrl.my_tree.select_parent_branch();
                var grandFatherBranch = ctrl.my_tree.select_parent_branch();
                path = "/" + grandFatherBranch.menu_data.path + "/" + fatherBranch.menu_data.path + "/" + ctrl.selectedBranch.menu_data.path;
            }
            ctrl.my_tree.set_select_branch_by_id(crtId);
            ctrl.updatePop(path, 'create');
        };

        ctrl.updateBranch = function () {
            ctrl.updatePop(ctrl.selectedBranch, 'update');
        }

        ctrl.delBranch = function () {
            var info = {};
            info.titleData = "提示";
            info.bodyData = "确定要删除菜单'" + ctrl.selectedBranch.menu_data.name + "'吗？";
            $rootScope.confirmWindow(info, function (data) {
                if (data) {
                    var info    = new Object();
                    info.id     = ctrl.selectedBranch.id;
                    info.req_id = ctrl.reqId != "" ? ctrl.reqId : "-1";
                    info.status = "DELETE";
                    saveMenu(info);
                }
            });
        }

        function saveModal(data, status) {
            data.menu_data.visible = data.menu_data.visible == "" ? "false" : "true";
            if (status == "create") {
                data.parent_id = ctrl.selectedBranch.id;
                data.req_id = ctrl.reqId ? ctrl.reqId : "-1";
                data.status = 'ADD';
                data.sys = ctrl.sys;
                data.level = parseInt(ctrl.selectedBranch.level) + 1;
            } else if (status == "update") {
                data.req_id = ctrl.reqId ? ctrl.reqId : "-1";
                data.status = 'UPDATE';
            }
            saveMenu(data);
        }

        function cancelModal(status) {
            //alert("您已经点击了取消按钮！");
        }

        function saveMenu(menu) {
            //menu.wsUuid = "172.16.3.192_5005_default";
            delete menu.children;
            delete menu.child;
			console.log("-----------menu----------");
			console.log(menu);
            $.ajax({
                type: 'post',
                url: matrix_product_url + '/menu/save',
                data: JSON.stringify(menu),
                contentType: "application/json; charset=utf-8",
                success: function (data) {
					console.log("-----------data---------------");
					console.log(data);
                    if (data.code == "0") {
                        ctrl.load();
                    } else {
                        $rootScope.showMessage('error', '失败！', data.desc);
                    }
                }
            })
        }

    }
});

app.controller("winitMenuPagePopCtrl", ["$scope", "$rootScope", '$modalInstance', 'info', 'status', function ($scope, $rootScope, $modalInstance, info, status) {
    $scope.verfPath = function () {
        angular.forEach(info.children, function (value, key) {
            if (value.path != null && value.path == $scope.menuTree.menuPath) {
                $rootScope.showMessage("warning", "警告！", "同级path属性不能重复！");
                return false;
            }
        });
    };
    if (status == "create" && status != "") {
        var randomId = 'MENU' + new Date().getTime();

        //给组件赋默认值
        $scope.menuTree = {
            id: randomId,
            is_del: "false",
            parent_id: "",
            req_id: "-1",
            status: "",
            sys: "",
            menu_data: {
                id: randomId,
                name: "",
                path: "",
                icon: "",
                routeId: info,
                rightId: "",
                visible: ""
            }
        };
    } else if (status == "update" && status != "") {
        console.log(JSON.stringify(info));
        $scope.menuTree = info;
    }
    $scope.createClick = function () {
        $modalInstance.close(status == "" ? info : $scope.menuTree);
    }
    $scope.cancelClick = function () {
        $modalInstance.dismiss('cancel');
    }
}]);